home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / layout / nsCSSValue.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  15KB  |  459 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsCSSValue_h___
  38. #define nsCSSValue_h___
  39.  
  40. #include "nsColor.h"
  41. #include "nsString.h"
  42. #include "nsCRT.h"
  43. #include "nsCoord.h"
  44. #include "nsCSSProperty.h"
  45. #include "nsUnitConversion.h"
  46. #include "nsIURI.h"
  47. #include "nsCOMPtr.h"
  48. #include "nsAutoPtr.h"
  49.  
  50. class imgIRequest;
  51. class nsIDocument;
  52.  
  53. enum nsCSSUnit {
  54.   eCSSUnit_Null         = 0,      // (n/a) null unit, value is not specified
  55.   eCSSUnit_Auto         = 1,      // (n/a) value is algorithmic
  56.   eCSSUnit_Inherit      = 2,      // (n/a) value is inherited
  57.   eCSSUnit_Initial      = 3,      // (n/a) value is default UA value
  58.   eCSSUnit_None         = 4,      // (n/a) value is none
  59.   eCSSUnit_Normal       = 5,      // (n/a) value is normal (algorithmic, different than auto)
  60.   eCSSUnit_String       = 10,     // (PRUnichar*) a string value
  61.   eCSSUnit_Attr         = 11,     // (PRUnichar*) a attr(string) value
  62.   eCSSUnit_Array        = 20,     // (nsCSSValue::Array*) a list of values
  63.   eCSSUnit_Counter      = 21,     // (nsCSSValue::Array*) a counter(string,[string]) value
  64.   eCSSUnit_Counters     = 22,     // (nsCSSValue::Array*) a counters(string,string[,string]) value
  65.   eCSSUnit_URL          = 30,     // (nsCSSValue::URL*) value
  66.   eCSSUnit_Image        = 31,     // (nsCSSValue::Image*) value
  67.   eCSSUnit_Integer      = 50,     // (int) simple value
  68.   eCSSUnit_Enumerated   = 51,     // (int) value has enumerated meaning
  69.   eCSSUnit_Color        = 80,     // (color) an RGBA value
  70.   eCSSUnit_Percent      = 90,     // (float) 1.0 == 100%) value is percentage of something
  71.   eCSSUnit_Number       = 91,     // (float) value is numeric (usually multiplier, different behavior that percent)
  72.  
  73.   // Length units - fixed
  74.   // US English
  75.   eCSSUnit_Inch         = 100,    // (float) 0.0254 meters
  76.   eCSSUnit_Foot         = 101,    // (float) 12 inches
  77.   eCSSUnit_Mile         = 102,    // (float) 5280 feet
  78.  
  79.   // Metric
  80.   eCSSUnit_Millimeter   = 207,    // (float) 1/1000 meter
  81.   eCSSUnit_Centimeter   = 208,    // (float) 1/100 meter
  82.   eCSSUnit_Meter        = 210,    // (float) Standard length
  83.   eCSSUnit_Kilometer    = 213,    // (float) 1000 meters
  84.  
  85.   // US Typographic
  86.   eCSSUnit_Point        = 300,    // (float) 1/72 inch
  87.   eCSSUnit_Pica         = 301,    // (float) 12 points == 1/6 inch
  88.  
  89.   // European Typographic
  90.   eCSSUnit_Didot        = 400,    // (float) 15 didots == 16 points
  91.   eCSSUnit_Cicero       = 401,    // (float) 12 didots
  92.  
  93.   // Length units - relative
  94.   // Font relative measure
  95.   eCSSUnit_EM           = 800,    // (float) == current font size
  96.   eCSSUnit_EN           = 801,    // (float) .5 em
  97.   eCSSUnit_XHeight      = 802,    // (float) distance from top of lower case x to baseline
  98.   eCSSUnit_CapHeight    = 803,    // (float) distance from top of uppercase case H to baseline
  99.   eCSSUnit_Char         = 804,    // (float) number of characters, used for width with monospace font
  100.  
  101.   // Screen relative measure
  102.   eCSSUnit_Pixel        = 900,    // (float) CSS pixel unit
  103.  
  104.   // Proportional Unit (for columns in tables)
  105.   eCSSUnit_Proportional = 950, 
  106.  
  107.   // Angular units
  108.   eCSSUnit_Degree       = 1000,    // (float) 360 per circle
  109.   eCSSUnit_Grad         = 1001,    // (float) 400 per circle
  110.   eCSSUnit_Radian       = 1002,    // (float) 2*pi per circle
  111.  
  112.   // Frequency units
  113.   eCSSUnit_Hertz        = 2000,    // (float) 1/seconds
  114.   eCSSUnit_Kilohertz    = 2001,    // (float) 1000 Hertz
  115.  
  116.   // Time units
  117.   eCSSUnit_Seconds      = 3000,    // (float) Standard time
  118.   eCSSUnit_Milliseconds = 3001     // (float) 1/1000 second
  119. };
  120.  
  121. class nsCSSValue {
  122. public:
  123.   struct Array;
  124.   friend struct Array;
  125.  
  126.   struct URL;
  127.   friend struct URL;
  128.  
  129.   struct Image;
  130.   friend struct Image;
  131.   
  132.   // for valueless units only (null, auto, inherit, none, normal)
  133.   explicit nsCSSValue(nsCSSUnit aUnit = eCSSUnit_Null)
  134.     : mUnit(aUnit)
  135.   {
  136.     NS_ASSERTION(aUnit <= eCSSUnit_Normal, "not a valueless unit");
  137.     if (aUnit > eCSSUnit_Normal) {
  138.       mUnit = eCSSUnit_Null;
  139.     }
  140.     mValue.mInt = 0;
  141.   }
  142.  
  143.   nsCSSValue(PRInt32 aValue, nsCSSUnit aUnit) NS_HIDDEN;
  144.   nsCSSValue(float aValue, nsCSSUnit aUnit) NS_HIDDEN;
  145.   nsCSSValue(const nsAString& aValue, nsCSSUnit aUnit) NS_HIDDEN;
  146.   explicit nsCSSValue(nscolor aValue) NS_HIDDEN;
  147.   nsCSSValue(Array* aArray, nsCSSUnit aUnit) NS_HIDDEN;
  148.   explicit nsCSSValue(URL* aValue) NS_HIDDEN;
  149.   explicit nsCSSValue(Image* aValue) NS_HIDDEN;
  150.   nsCSSValue(const nsCSSValue& aCopy) NS_HIDDEN;
  151.   NS_FASTCALL ~nsCSSValue() NS_HIDDEN;
  152.  
  153.   NS_HIDDEN_(nsCSSValue&)  operator=(const nsCSSValue& aCopy);
  154.   NS_HIDDEN_(PRBool)      operator==(const nsCSSValue& aOther) const;
  155.  
  156.   PRBool operator!=(const nsCSSValue& aOther) const
  157.   {
  158.     return !(*this == aOther);
  159.   }
  160.  
  161.   nsCSSUnit GetUnit() const { return mUnit; };
  162.   PRBool    IsLengthUnit() const
  163.     { return PRBool((eCSSUnit_Inch <= mUnit) && (mUnit <= eCSSUnit_Proportional)); }
  164.   PRBool    IsFixedLengthUnit() const  
  165.     { return PRBool((eCSSUnit_Inch <= mUnit) && (mUnit <= eCSSUnit_Cicero)); }
  166.   PRBool    IsRelativeLengthUnit() const  
  167.     { return PRBool((eCSSUnit_EM <= mUnit) && (mUnit <= eCSSUnit_Proportional)); }
  168.   PRBool    IsAngularUnit() const  
  169.     { return PRBool((eCSSUnit_Degree <= mUnit) && (mUnit <= eCSSUnit_Radian)); }
  170.   PRBool    IsFrequencyUnit() const  
  171.     { return PRBool((eCSSUnit_Hertz <= mUnit) && (mUnit <= eCSSUnit_Kilohertz)); }
  172.   PRBool    IsTimeUnit() const  
  173.     { return PRBool((eCSSUnit_Seconds <= mUnit) && (mUnit <= eCSSUnit_Milliseconds)); }
  174.  
  175.   PRInt32 GetIntValue() const
  176.   {
  177.     NS_ASSERTION(mUnit == eCSSUnit_Integer || mUnit == eCSSUnit_Enumerated,
  178.                  "not an int value");
  179.     return mValue.mInt;
  180.   }
  181.  
  182.   float GetPercentValue() const
  183.   {
  184.     NS_ASSERTION(mUnit == eCSSUnit_Percent, "not a percent value");
  185.     return mValue.mFloat;
  186.   }
  187.  
  188.   float GetFloatValue() const
  189.   {
  190.     NS_ASSERTION(eCSSUnit_Number <= mUnit, "not a float value");
  191.     return mValue.mFloat;
  192.   }
  193.  
  194.   nsAString& GetStringValue(nsAString& aBuffer) const
  195.   {
  196.     NS_ASSERTION(eCSSUnit_String <= mUnit && mUnit <= eCSSUnit_Attr,
  197.                  "not a string value");
  198.     aBuffer.Truncate();
  199.     if (nsnull != mValue.mString) {
  200.       aBuffer.Append(mValue.mString);
  201.     }
  202.     return aBuffer;
  203.   }
  204.  
  205.   const PRUnichar* GetStringBufferValue() const
  206.   {
  207.     NS_ASSERTION(eCSSUnit_String <= mUnit && mUnit <= eCSSUnit_Attr,
  208.                  "not a string value");
  209.     return mValue.mString;
  210.   }
  211.  
  212.   nscolor GetColorValue() const
  213.   {
  214.     NS_ASSERTION((mUnit == eCSSUnit_Color), "not a color value");
  215.     return mValue.mColor;
  216.   }
  217.  
  218.   Array* GetArrayValue() const
  219.   {
  220.     NS_ASSERTION(eCSSUnit_Array <= mUnit && mUnit <= eCSSUnit_Counters,
  221.                  "not an array value");
  222.     return mValue.mArray;
  223.   }
  224.  
  225.   nsIURI* GetURLValue() const
  226.   {
  227.     NS_ASSERTION(mUnit == eCSSUnit_URL || mUnit == eCSSUnit_Image,
  228.                  "not a URL value");
  229.     return mUnit == eCSSUnit_URL ?
  230.       mValue.mURL->mURI : mValue.mImage->mURI;
  231.   }
  232.  
  233.   const PRUnichar* GetOriginalURLValue() const
  234.   {
  235.     NS_ASSERTION(mUnit == eCSSUnit_URL || mUnit == eCSSUnit_Image,
  236.                  "not a URL value");
  237.     return mUnit == eCSSUnit_URL ?
  238.       mValue.mURL->mString : mValue.mImage->mString;
  239.   }
  240.  
  241.   // Not making this inline because that would force us to include
  242.   // imgIRequest.h, which leads to REQUIRES hell, since this header is included
  243.   // all over.
  244.   NS_HIDDEN_(imgIRequest*) GetImageValue() const;
  245.  
  246.   NS_HIDDEN_(nscoord)   GetLengthTwips() const;
  247.  
  248.   NS_HIDDEN_(void)  Reset()  // sets to null
  249.   {
  250.     if ((eCSSUnit_String <= mUnit) && (mUnit <= eCSSUnit_Attr) &&
  251.         (nsnull != mValue.mString)) {
  252.       nsCRT::free(mValue.mString);
  253.     } else if (eCSSUnit_Array <= mUnit && mUnit <= eCSSUnit_Counters) {
  254.       mValue.mArray->Release();
  255.     } else if (eCSSUnit_URL == mUnit) {
  256.       mValue.mURL->Release();
  257.     } else if (eCSSUnit_Image == mUnit) {
  258.       mValue.mImage->Release();
  259.     }
  260.     mUnit = eCSSUnit_Null;
  261.     mValue.mInt = 0;
  262.   }
  263.  
  264.   NS_HIDDEN_(void)  SetIntValue(PRInt32 aValue, nsCSSUnit aUnit);
  265.   NS_HIDDEN_(void)  SetPercentValue(float aValue);
  266.   NS_HIDDEN_(void)  SetFloatValue(float aValue, nsCSSUnit aUnit);
  267.   NS_HIDDEN_(void)  SetStringValue(const nsAString& aValue, nsCSSUnit aUnit);
  268.   NS_HIDDEN_(void)  SetColorValue(nscolor aValue);
  269.   NS_HIDDEN_(void)  SetArrayValue(nsCSSValue::Array* aArray, nsCSSUnit aUnit);
  270.   NS_HIDDEN_(void)  SetURLValue(nsCSSValue::URL* aURI);
  271.   NS_HIDDEN_(void)  SetImageValue(nsCSSValue::Image* aImage);
  272.   NS_HIDDEN_(void)  SetAutoValue();
  273.   NS_HIDDEN_(void)  SetInheritValue();
  274.   NS_HIDDEN_(void)  SetInitialValue();
  275.   NS_HIDDEN_(void)  SetNoneValue();
  276.   NS_HIDDEN_(void)  SetNormalValue();
  277.   NS_HIDDEN_(void)  StartImageLoad(nsIDocument* aDocument,
  278.                                    PRBool aIsBGImage = PR_FALSE)
  279.                                    const;  // Not really const, but pretending
  280.  
  281. #ifdef DEBUG
  282.   NS_HIDDEN_(void)
  283.     AppendToString(nsAString& aBuffer,
  284.                    nsCSSProperty aPropID = eCSSProperty_UNKNOWN) const;
  285.   NS_HIDDEN_(void)
  286.     ToString(nsAString& aBuffer,
  287.              nsCSSProperty aPropID = eCSSProperty_UNKNOWN) const;
  288. #endif
  289.  
  290.   MOZ_DECL_CTOR_COUNTER(nsCSSValue::Array)
  291.  
  292.   struct Array {
  293.  
  294.     // return |Array| with reference count of zero
  295.     static Array* Create(PRUint16 aItemCount) {
  296.       return new (aItemCount) Array(aItemCount);
  297.     }
  298.  
  299.     nsCSSValue& operator[](PRUint16 aIndex) {
  300.       NS_ASSERTION(aIndex < mCount, "out of range");
  301.       return *(First() + aIndex);
  302.     }
  303.  
  304.     const nsCSSValue& operator[](PRUint16 aIndex) const {
  305.       NS_ASSERTION(aIndex < mCount, "out of range");
  306.       return *(First() + aIndex);
  307.     }
  308.  
  309.     nsCSSValue& Item(PRUint16 aIndex) { return (*this)[aIndex]; }
  310.     const nsCSSValue& Item(PRUint16 aIndex) const { return (*this)[aIndex]; }
  311.  
  312.     PRUint16 Count() { return mCount; }
  313.  
  314.     PRBool operator==(const Array& aOther)
  315.     {
  316.       if (mCount != aOther.mCount)
  317.         return PR_FALSE;
  318.       for (PRUint16 i = 0; i < mCount; ++i)
  319.         if ((*this)[i] != aOther[i])
  320.           return PR_FALSE;
  321.       return PR_TRUE;
  322.     }
  323.  
  324.     void AddRef() {
  325.       ++mRefCnt;
  326.       NS_LOG_ADDREF(this, mRefCnt, "nsCSSValue::Array", sizeof(*this));
  327.     }
  328.     void Release() {
  329.       --mRefCnt;
  330.       NS_LOG_RELEASE(this, mRefCnt, "nsCSSValue::Array");
  331.       if (mRefCnt == 0)
  332.         delete this;
  333.     }
  334.  
  335.   private:
  336.  
  337.     PRUint16 mRefCnt;
  338.     PRUint16 mCount;
  339.  
  340.     void* operator new(size_t aSelfSize, PRUint16 aItemCount) CPP_THROW_NEW {
  341.       return ::operator new(aSelfSize + sizeof(nsCSSValue)*aItemCount);
  342.     }
  343.  
  344.     void operator delete(void* aPtr) { ::operator delete(aPtr); }
  345.  
  346.     nsCSSValue* First() {
  347.       return (nsCSSValue*) (((char*)this) + sizeof(*this));
  348.     }
  349.  
  350.     const nsCSSValue* First() const {
  351.       return (const nsCSSValue*) (((const char*)this) + sizeof(*this));
  352.     }
  353.  
  354. #define CSSVALUE_LIST_FOR_VALUES(var)                                         \
  355.   for (nsCSSValue *var = First(), *var##_end = var + mCount;                  \
  356.        var != var##_end; ++var)
  357.  
  358.     Array(PRUint16 aItemCount)
  359.       : mRefCnt(0)
  360.       , mCount(aItemCount)
  361.     {
  362.       MOZ_COUNT_CTOR(nsCSSValue::Array);
  363.       CSSVALUE_LIST_FOR_VALUES(val) {
  364.         new (val) nsCSSValue();
  365.       }
  366.     }
  367.  
  368.     ~Array()
  369.     {
  370.       MOZ_COUNT_DTOR(nsCSSValue::Array);
  371.       CSSVALUE_LIST_FOR_VALUES(val) {
  372.         val->~nsCSSValue();
  373.       }
  374.     }
  375.  
  376. #undef CSSVALUE_LIST_FOR_VALUES
  377.  
  378.   private:
  379.     Array(const Array& aOther); // not to be implemented
  380.   };
  381.  
  382.   MOZ_DECL_CTOR_COUNTER(nsCSSValue::URL)
  383.  
  384.   struct URL {
  385.     // Caller must delete this object immediately if the allocation of
  386.     // |mString| fails.
  387.     URL(nsIURI* aURI, const PRUnichar* aString, nsIURI* aReferrer)
  388.       : mURI(aURI),
  389.         mString(nsCRT::strdup(aString)),
  390.         mReferrer(aReferrer),
  391.         mRefCnt(0)
  392.     {
  393.       MOZ_COUNT_CTOR(nsCSSValue::URL);
  394.     }
  395.  
  396.     ~URL()
  397.     {
  398.       // null |mString| isn't valid normally, but is checked by callers
  399.       // of the constructor
  400.       if (mString)
  401.         nsCRT::free(mString);
  402.       MOZ_COUNT_DTOR(nsCSSValue::URL);
  403.     }
  404.  
  405.     PRBool operator==(const URL& aOther)
  406.     {
  407.       PRBool eq;
  408.       return nsCRT::strcmp(mString, aOther.mString) == 0 &&
  409.              (mURI == aOther.mURI || // handles null == null
  410.               (mURI && aOther.mURI &&
  411.                NS_SUCCEEDED(mURI->Equals(aOther.mURI, &eq)) &&
  412.                eq));
  413.     }
  414.  
  415.     nsCOMPtr<nsIURI> mURI; // null == invalid URL
  416.     PRUnichar* mString;
  417.     nsCOMPtr<nsIURI> mReferrer;
  418.  
  419.     void AddRef() { ++mRefCnt; }
  420.     void Release() { if (--mRefCnt == 0) delete this; }
  421.   protected:
  422.     nsrefcnt mRefCnt;
  423.   };
  424.  
  425.   MOZ_DECL_CTOR_COUNTER(nsCSSValue::Image)
  426.  
  427.   struct Image : public URL {
  428.     // Not making the constructor and destructor inline because that would
  429.     // force us to include imgIRequest.h, which leads to REQUIRES hell, since
  430.     // this header is included all over.
  431.     Image(nsIURI* aURI, const PRUnichar* aString, nsIURI* aReferrer,
  432.           nsIDocument* aDocument, PRBool aIsBGImage = PR_FALSE) NS_HIDDEN;
  433.     ~Image() NS_HIDDEN;
  434.  
  435.     // Inherit operator== from nsCSSValue::URL
  436.  
  437.     nsCOMPtr<imgIRequest> mRequest; // null == image load blocked or somehow failed
  438.  
  439.     // Override AddRef/Release so we delete ourselves via the right pointer.
  440.     void AddRef() { ++mRefCnt; }
  441.     void Release() { if (--mRefCnt == 0) delete this; }
  442.   };
  443.  
  444. protected:
  445.   nsCSSUnit mUnit;
  446.   union {
  447.     PRInt32    mInt;
  448.     float      mFloat;
  449.     PRUnichar* mString;
  450.     nscolor    mColor;
  451.     Array*     mArray;
  452.     URL*       mURL;
  453.     Image*     mImage;
  454.   }         mValue;
  455. };
  456.  
  457. #endif /* nsCSSValue_h___ */
  458.  
  459.